import 'package:flutter/material.dart';
import 'package:yqmssa_app/config/app_config.dart';
import 'package:yqmssa_app/utils/tencent_im_utils.dart';
import 'package:yqmssa_app/views/index/screen/home_screen.dart';
import 'package:yqmssa_app/views/index/screen/message_manager.dart';
import 'package:yqmssa_app/views/index/screen/message_screen.dart';
import 'package:yqmssa_app/views/index/screen/mine_screen.dart';
import 'package:get/get.dart';

class IndexView extends StatefulWidget {
  const IndexView({super.key});

  @override
  State<IndexView> createState() => _IndexViewState();
}

class _IndexViewState extends State<IndexView> {
  final screens = const [
    HomeScreen(),
    MessageScreen(),
    MineScreen(),
  ];

  final bI = [
    BottomNavigationBarItem(
      icon: Image.asset(AppConfig.imagePath("home.png"), width: 28),
      label: "首页",
      activeIcon: Image.asset(AppConfig.imagePath("home_sel.png"), width: 28),
    ),
    BottomNavigationBarItem(
      icon: Image.asset(AppConfig.imagePath("message.png"), width: 28),
      label: "消息",
      activeIcon: Image.asset(AppConfig.imagePath("message_sel.png"), width: 28),
    ),
    BottomNavigationBarItem(
      icon: Image.asset(AppConfig.imagePath("mine.png"), width: 28),
      label: "我的",
      activeIcon: Image.asset(AppConfig.imagePath("mine_sel.png"), width: 28),
    ),
  ];

  int _currentIndex = 0;

  @override
  void initState() {
    super.initState();
    tencentImUtils.init();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: const Color(0XFFF8FAFD),
      body: IndexedStack(
        index: _currentIndex,
        children: screens,
      ),
      bottomNavigationBar: Stack(
        children: [
          BottomNavigationBar(
            items: bI,
            backgroundColor: Colors.white,
            currentIndex: _currentIndex,
            unselectedItemColor: const Color(0XFF999999),
            selectedItemColor: const Color(0XFF4A1C02),
            selectedFontSize: 10,
            unselectedFontSize: 10,
            elevation: 0,
            onTap: (index) {
              // final manager = Get.find<HomeManager>();
              // if (index != 0) {
              //   manager.betterPlayerController.value.pause();
              // } else {
              //   manager.betterPlayerController.value.play();
              // }
              setState(() {
                _currentIndex = index;
              });
            },
          ),
          Positioned(
            left: (MediaQuery.of(context).size.width / 2) + 6,
            child: Builder(
              builder: (context) {
                final m = Get.find<MessageManager>();
                return Obx(() {
                  return Badge(
                    label: Text("${m.unRead.value + m.msgUnread.value}"),
                    isLabelVisible: (m.unRead.value + m.msgUnread.value) > 0,
                    backgroundColor: Colors.red,
                  );
                });
              },
            ),
          ),
        ],
      ),
    );
  }

  @override
  void dispose() {
    tencentImUtils.logout();
    super.dispose();
  }
}
